package com.ak.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class LoginService implements UserDetailsService {



    @Autowired
    PasswordEncoder passwordEncoder;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 自定义 用户登录名
        if(!(username.equals("admin")||username.equals("vip1")||username.equals("vip2"))){
            throw  new UsernameNotFoundException("这是俺自己写的错误");
        }

        if(username.equals("vip1")){
            // 自定义用户密码
            String password=passwordEncoder.encode("123");
            return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,vip1"));
        }
        if(username.equals("vip2")){
            // 自定义用户密码
            String password=passwordEncoder.encode("123");
            return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,vip2"));
        }
        // 自定义用户密码
        String password=passwordEncoder.encode("123");
        return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_abc"));
    }


//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//
//        // 自定义 用户登录名
//        if(!(username.equals("admin")||username.equals("vip1")||username.equals("vip2"))){
//            throw  new UsernameNotFoundException("这是俺自己写的错误");
//        }
//
//        if(username.equals("vip1")){
//            // 自定义用户密码
//            String password=passwordEncoder.encode("123");
//            return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,vip1"));
//        }
//        if(username.equals("vip2")){
//            // 自定义用户密码
//            String password=passwordEncoder.encode("123");
//            return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,vip2"));
//        }
//        // 自定义用户密码
//        String password=passwordEncoder.encode("123");
//        return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
//    }
}
